<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <th:block th:include="common/include :: header('结算单明细')" />
</head>
<body>
    <div class="ui-layout-center">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                <div class="col-sm-12" >
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">客户姓名：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerName}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">联系方式：</label>
                            <label class="col-sm-6 control-label">[[${statement.customerPhone}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12" >

                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">车牌号码：</label>
                            <label class="col-sm-6 control-label">[[${statement.licensePlate}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">汽车类型：</label>
                            <label class="col-sm-6 control-label">[[${statement.carSeries}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12" >
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">服务类型：</label>
                            <label class="col-sm-6 control-label">[[${@dict.getLabel('si_service_catalog',statement.serviceType)}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">到店时间：</label>
                            <label class="col-sm-6 control-label" th:text="${#dates.format(statement.actualArrivalTime, 'yyyy-MM-dd HH:mm:ss')}"></label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12" >
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">总消费金额：</label>
                            <label class="col-sm-6 control-label" onchange="updateAmount()" id="totalAmount">[[${statement.totalAmount}]]</label>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">实付价格：</label>
                            <label class="col-sm-6 control-label" id="actuallyPaid">[[${statement.totalAmount-statement.discountAmount}]]</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12" >
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-6 control-label">优惠价格：</label>
                            <input class="col-sm-6" type="number" onchange="updateAmount()" name="discountAmount" th:field="${statement.discountAmount}"/>
                        </div>
                    </div>
                </div>
            </div>
                <div class="btn-group-sm" id="item-toolbar" role="group">
                    <a id="saveBtn" class="btn btn-success " onclick="saveFun()" shiro:hasPermission="business:statementItem:add">
                        <i class="fa fa-plus"></i> 保存
                    </a>
                    <a id="payBtn" class="btn btn-info disabled" onclick="payFun()" shiro:hasPermission="business:statementItem:payStatement">
                        <i class="fa fa-cc-visa "></i> 确认支付
                    </a>
                </div>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="statementItemTable"></table>
                    </div>
                </div>
            </div>
    </div>
    </div>
    <div class="ui-layout-east">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="serviceItemFormId">
                        <div class="select-list">
                            <ul>
                                <li>
                                    服务项名称：<input type="text" name="name"/>
                                </li>
                                <li>
                                    套餐否
                                    <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                        <option value="">所有</option>
                                        <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                                    </select>
                                </li>
                                <li>
                                    服务分类
                                    <select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                                        <option value="">所有</option>
                                        <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                                    </select>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>
                <div class="col-sm-12 select-table table-striped">
                    <table id="serviceItemTable"></table>
                </div>
            </div>
        </div>
    </div>
</body>
<th:block th:include="common/include :: footer" />
<th:block th:include="common/include :: layout-latest" />
<script th:inline="javascript">
    $('body').layout({
        east__size:	'30%',
    });

    $(function() {
        var options = {
            id:"serviceItemTable",
            url: "/business/serviceItem/query?saleStatus="+1,
            uniqueId:"id",
            modalName: "服务项",
            columns: [
                {
                    field: 'id',
                    title: '服务项id',
                    visible: false
                },

                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },

                {
                    field: 'originalPrice',
                    title: '服务项原价',
                    sortable: true
                },

                {
                    field: 'info',
                    title: '备注信息',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.tooltip(value, 0, "open");
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a  class="btn btn-success btn-xs " href="javascript:void(0)" ' +
                            'onclick="addItems(\'' + row.id + '\')"><i class="fa fa-plus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    var statementId = [[${statement.id}]];
    $(function() {
        var options = {
            id:"statementItemTable",
            uniqueId:'itemId',
            toolbar:'item-toolbar',
            url: "/business/statementItem/query?statementId="+statementId,
            modalName: "结算单明细",
            columns: [
                {
                    field: 'id',
                    title: '结算单id',
                    visible: false
                },
                {
                    field: 'itemId',
                    title: '服务项明细id',
                    sortable: true
                },
                {
                    field: 'itemName',
                    title: '服务项明细名称',
                    sortable: true
                },
                {
                    field: 'itemPrice',
                    title: '服务项价格',
                    sortable: true
                },
                {
                    field: 'itemQuantity',
                    title: '购买数量',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a  class="btn btn-success btn-xs " href="javascript:void(0)" ' +
                            'onclick="addSelf(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a> ');
                        actions.push('<a  class="btn btn-danger btn-xs " href="javascript:void(0)" ' +
                            'onclick="deleteSelf(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a> ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
    var $serviceItemRow = $('#serviceItemTable');
    var $statementItemRow = $('#statementItemTable');

    function addItems(id) {
        var data = $serviceItemRow.bootstrapTable('getRowByUniqueId', id);
        var statementData = $statementItemRow.bootstrapTable('getRowByUniqueId', id);
        var newData = {itemId: data.id, itemName: data.name, itemPrice: data.originalPrice, itemQuantity: 1};
        if (statementData) {
            $statementItemRow.bootstrapTable('updateCellByUniqueId', {
                id: id,
                field: 'itemQuantity',
                value: statementData.itemQuantity + 1
            })
        } else {
            $statementItemRow.bootstrapTable('append', newData);
        }
        updateAmount();
        $("#payBtn").addClass("disabled");
        $("#saveBtn").removeClass("disabled");
    }


    function addSelf(id) {
        var statementData = $statementItemRow.bootstrapTable('getRowByUniqueId', id);
        $statementItemRow.bootstrapTable('updateCellByUniqueId', {
            id: id,
            field: 'itemQuantity',
            value: statementData.itemQuantity + 1
        });
        $("#payBtn").addClass("disabled");
        $("#saveBtn").removeClass("disabled");
        updateAmount();
    }

    function deleteSelf(id) {

        var statementData = $statementItemRow.bootstrapTable('getRowByUniqueId', id);
        if (statementData.itemQuantity > 1) {
            $statementItemRow.bootstrapTable('updateCellByUniqueId', {
                id: id,
                field: 'itemQuantity',
                value: statementData.itemQuantity - 1
            })
        } else {
            $statementItemRow.bootstrapTable('removeByUniqueId', id);

        }
        $("#payBtn").addClass("disabled");
        $("#saveBtn").removeClass("disabled");
        updateAmount();
    }


    //true是单个页数
    /*var discountAmount =0;
    function discountFun() {
        discountAmount=$("input[name='discountAmount']").val()
        updateAmount();
    }*/
    var $totalAmount = $("#totalAmount");
    var $actuallyPaid = $("#actuallyPaid");
    var $discountAmount =$("input[name='discountAmount']");
    function updateAmount(){
        var actuallyPaid=0;
        var totalAmount=0;
        var discountAmount;
        //获取整个表格的数据,false是所有页数
        var data= $statementItemRow.bootstrapTable('getData',false);
        $.each(data,function (index,item) {
            if (item.itemId){
                totalAmount += item.itemPrice*item.itemQuantity;
            }
        });

        /*if (!data.length){
            $totalAmount.html(0);
            $actuallyPaid.html(0);
            $discountAmount.val(0);
            return;
        }*/
        discountAmount = $discountAmount.val();
        if (!discountAmount) {
            $discountAmount.val(0);
            return;
        }
        if (discountAmount>totalAmount){
            $discountAmount.val(0);
            $actuallyPaid.html(totalAmount);
            return;
        }
        if (discountAmount<0){
            $discountAmount.val(0);
            return;
        }
        $totalAmount.html(totalAmount);
        $actuallyPaid.html(totalAmount-discountAmount);
    }

    /**
     * 需要将一个是statementId 和折扣价封装进去,由于需要传入折扣价格,方便在后端的代码中计算总金额
     * 问题1:而statementItem对象中并没有优惠金额的字段所以是无法封装进去
     * 解决方法,后端生成一个类,接收穿过的一个是装statementItem的集合对象和字段discountAmount
     * 还有一种就是将discountAmount封装到一个statementItem对像中,形成一个list集合一起传过去
     *
     */
    function saveFun() {
        $.modal.confirm("确定要保存",function () {

        var discountAmount = $discountAmount.val();
        var statementId = [[${statement.id}]];
        var data= $statementItemRow.bootstrapTable('getData',false);
        data.push({"itemPrice":discountAmount,"statementId":statementId})

            config={
                url:"/business/statementItem/saveItems",
                type:'post',
                dataType:'json',
                contentType:"application/json",
                data:JSON.stringify(data),
                beforeSend:function () {
                    $.modal.loading("正在处理....");
                },
                success:function (result) {
                    $.operate.ajaxSuccess(result);
                }
            };
            $.ajax(config);
                $("#saveBtn").addClass("disabled");
                $("#payBtn").removeClass("disabled");
                $.table.refresh("#statementItemTable");
            });
    }
    function translation(id) {
        window.location.href="/business/statementItem/editOrDetail?statementId="+id;
        $.table.refresh("bootstrap-table");
    }
     function payFun(){
         $.modal.confirm("确定要支付么",function () {
             var statementId = [[${statement.id}]];
             config={
                 url:"/business/statementItem/pay?statementId="+statementId,
                 type:'post',
                 dataType:'json',
                 contentType:"application/json",
                 beforeSend:function () {
                     $.modal.loading("正在处理....");
                 },
                 success:function (result) {
                     $.operate.ajaxSuccess(result);
                     translation(statementId);
                 }
             };
             $.ajax(config);
         });
     }
</script>
</html>